import 'package:kmxy_base/base/base_package.dart';
import 'package:kmxy_base/page/map_select/map_select_page_state.dart';

/// 地图位置选择
// ignore: must_be_immutable
class MapSelectPage extends BasePage {
  MapSelectPage({Key? key, arguments}) : super(key: key, arguments: arguments);
  MapSelectPageView createState() => MapSelectPageView();
}

class MapSelectPageView extends MapSelectPageState {
  @override
  Widget setPageContent(BuildContext context) {
    initMap();
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: widget.arguments?['titleColor'] ?? Colors.white,
        title: Text(
          '位置选择',
          style: TextStyle(
            color: widget.arguments?['titleTextColor'] ?? Colors.black,
            fontSize: 16.sp,
            fontWeight: FontWeight.w700,
          ),
        ),
        actions: [
          InkWell(
            onTap: sure,
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.all(16.w),
              child: Text(
                '确定',
                style: TextStyle(
                  color: widget.arguments?['titleTextColor'] ?? Colors.black,
                ),
              ),
            ),
          )
        ],
      ),
      body: SafeArea(
        child: Column(
          children: [
            SizedBox(
              height: 300.w,
              width: widthScreen,
              child: Stack(
                children: [
                  map,
                  Center(
                    child:
                        Icon(Icons.location_on, size: 26.w, color: Colors.red),
                  ),
                  Positioned(
                    right: 10.w,
                    bottom: 10.w,
                    child: InkWell(
                      onTap: location,
                      child: Container(
                        padding: EdgeInsets.all(8.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          // 圆角设置
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: const Icon(Icons.my_location_sharp),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: GridListView(
                addressData.isEmpty ? [] : addressData['pois'],
                (context, index, itemData) =>
                    itemView(context, index, itemData),
                onGridListViewCreated,
                onRefresh: () => {},
                onLoading: () => {},
                enablePullDown: false,
                enablePullUp: false,
                divider: const Divider(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget itemView(BuildContext context, int index, Map itemData) {
    if (itemData['select'] == null) {
      if (index == 0) {
        List latlng = itemData['location'].split(',');
        lttd = double.parse(latlng[1]);
        lgtd = double.parse(latlng[0]);
        address =
            '${addressData['addressComponent']['province']}${addressData['addressComponent']['city']}${addressData['addressComponent']['district']}${itemData['businessarea']}${itemData['name']}'
                .replaceAll('[', '')
                .replaceAll(']', '');
        itemData['select'] = true;
      } else {
        itemData['select'] = false;
      }
    }
    return InkWell(
      onTap: () => itemClick(itemData),
      child: Container(
        padding: EdgeInsets.all(10.w),
        decoration: BoxDecoration(
          color: Colors.white,
          // 圆角设置
          borderRadius: BorderRadius.circular(8),
        ),
        child: Row(
          children: [
            SizedBox(
              width: 320.w,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '${itemData['name']}',
                    style:
                        TextStyle(fontWeight: FontWeight.bold, fontSize: 16.w),
                  ),
                  Text(
                    '${itemData['distance']}m | ${itemData['address']}',
                    style: TextStyle(fontSize: 14.w),
                  ),
                ],
              ),
            ),
            Expanded(child: Container()),
            itemData['select']
                ? const Icon(
                    Icons.check,
                    color: Color(0xff4B7AEF),
                  )
                : Container(),
          ],
        ),
      ),
    );
  }
}
